﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Utility;
using WPay.Domain.MainModule.Interface.DomainServices;
using WPay.Domain.MainModule.Interface.RepositoryServices;
using WPay.Domain.Model.DTOs;
using WPay.Domain.Model.Entities;
using WPay.Infrastructure.Unity;

namespace WPay.Web.API.Controllers
{
    /// <summary>
    /// 文件服务接口
    /// </summary>
    public class FileController : BaseController
    {
     
        private readonly ICpn_RepositoryService _cpn_RepositoryService;
        /// <summary>
        /// 文件服务接口
        /// </summary>
        /// <param name="difords_DomainService"></param>
        /// <param name="channel_DomainService"></param>
        /// <param name="cpn_RepositoryService"></param>
        public FileController(ICpn_RepositoryService cpn_RepositoryService)
        {
           
            _cpn_RepositoryService = cpn_RepositoryService;
        }
       

       

       
        /// <summary>
        /// 上传图片，参数只传CpnID[企业号],文件通过正常的form-Data的形式上传，具体参考网页form数据上传格式
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public RespDto UploadPic()
        {
            var b = false;
            string hs = String.Empty;
            try
            {
                var files = Request.Form.Files;
                if (files.Count == 0)
                {
                    throw new UNException("未找到需要上传的文件");
                }
                hs = AwsHelper.FileToS3ByHttpPostedFile(files[0], UploadFileHelper.DataType.Image, UploadFileHelper.DataBusiness.Other);
                b = !string.IsNullOrWhiteSpace(hs);
            }
            catch (Exception ex)
            {
                ErrorLog.WriteLog("Guest-UnLoadPic-Exception", JSONSerializer.Serialize(ex));
            }
            return new RespDto().RunWithTry(x =>
            {
                x.Data = hs;
                x.Success = b;
            });
        }
       
    }
}